header%20ipynb.png

Hands-On

Hands-On ini digunakan pada kegiatan Microcredential Associate Data Scientist 2021

Memulai Python!

Dapat menuliskan keterangan kode menggunakan Markdown

In [1]:
print("Hello world!")   # mode skrip
Hello world!
In [2]:
height = 1.84
In [3]:
tall = True
In [4]:
height1 = 1.84
In [5]:
height2 = 1.79
In [6]:
height3 = 1.82
In [7]:
height4 = 1.90

Masalah :

  • Terlalu banyak data masukan untuk tipe data yang sama
  • Tidak nyaman
  • Solusi: Python List
In [8]:
[1.84, 1.79, 1.82, 1.90, 1.80]
Out[8]:
[1.84, 1.79, 1.82, 1.9, 1.8]
In [9]:
height = [1.84, 1.79, 1.82, 1.90, 1.80]
In [10]:
height
Out[10]:
[1.84, 1.79, 1.82, 1.9, 1.8]
In [11]:
famz = ["Abe", 1.84, "Beb", 1.79, "Cory", 1.82, "Dad", 1.90]
In [12]:
famz
Out[12]:
['Abe', 1.84, 'Beb', 1.79, 'Cory', 1.82, 'Dad', 1.9]
In [13]:
weight = [66.5, 60.3, 64.7, 89.5, 69.8] 
In [14]:
weight
Out[14]:
[66.5, 60.3, 64.7, 89.5, 69.8]
In [15]:
weight / height ** 2 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_19700/1732703633.py in <module>
----> 1 weight / height ** 2

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

Solusi: NumPy

  • Library dasar untuk perhitungan saintifik (scientific computing) dengan Python (https://numpy.org/)
  • Alternatif untuk Python List: Numpy Array untuk n-dimensi
  • Mudah digunakan dan bersifat open source
  • Jika library belum terpasang, tuliskan perintah instalasi: pip install numpy
  • Kemudian impor: import numpy as np
In [16]:
import numpy as np
In [17]:
np_height = np.array(height)
In [18]:
np_height 
Out[18]:
array([1.84, 1.79, 1.82, 1.9 , 1.8 ])
In [19]:
np_weight = np.array(weight)
In [20]:
np_weight
Out[20]:
array([66.5, 60.3, 64.7, 89.5, 69.8])
In [21]:
bmi = np_weight / np_height ** 2
In [22]:
bmi
Out[22]:
array([19.64201323, 18.81963734, 19.53266514, 24.79224377, 21.54320988])

Untuk melihat fungsi lain pada NumPy, gunakan perintah np.< TAB >

In [23]:
np.
  File "C:\Users\andii\AppData\Local\Temp/ipykernel_19700/2469254449.py", line 1
    np.
       ^
SyntaxError: invalid syntax
In [24]:
np_height = np.array([1.84, 1.79, 1.82, 1.9, 1.8])
In [25]:
np_weight = np.array([66.5, 60.3, 64.7, 89.5, 69.8])
In [26]:
type(np_height)
Out[26]:
numpy.ndarray
In [27]:
type(np_weight)
Out[27]:
numpy.ndarray
In [28]:
np_2d = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
In [29]:
np_2d
Out[29]:
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])
In [30]:
np_2d.shape
Out[30]:
(2, 5)

SciPy

  • SciPy (dibaca “Sigh Pie”) merupakan library yang bersifat open source dan tersedia di https://www.scipy.org/
  • SciPy dibangun untuk untuk bekerja dengan NumPy array dan menyediakan kumpulan algoritma numerik, termasuk pemrosesan sinyal, optimasi, statistika, dan library Matplotlib untuk visualisasi data.
  • Jika library belum terpasang, tuliskan perintah instalasi: pip install scipy

Pandas

  • Pandas (Panel Data) merupakan library popular di Python yang digunakan untuk data structure dan data analysis
  • Bersifat open source dan tersedia di https://pandas.pydata.org/
  • Pandas sangat berkaitan dengan NumPy
  • Jika library belum terpasang, tuliskan perintah instalasi: pip install pandas
  • Kemudian impor: import pandas as pd
In [31]:
#  series
np.array([1, 2, 3, 4, 5])
Out[31]:
array([1, 2, 3, 4, 5])
In [32]:
# DataFrame
np.array([[1, 2], [3, 4]])
Out[32]:
array([[1, 2],
       [3, 4]])
In [33]:
import pandas as pd
In [34]:
Tab = pd.read_csv("Tab.csv")
In [35]:
Tab
Out[35]:
Unnamed: 0 Negara Populasi Area Ibukota
0 IN Indonesia 250 123456 Jakarta
1 MA Malaysia 25 3456 KL
2 SI Singapura 15 456 Singapura
3 JP Jepang 60 5678 Tokyo
4 TH Thailand 45 678 Bangkok
In [36]:
Tab["Negara"]
Out[36]:
0    Indonesia
1     Malaysia
2    Singapura
3       Jepang
4     Thailand
Name: Negara, dtype: object
In [37]:
Tab.Ibukota
Out[37]:
0      Jakarta
1           KL
2    Singapura
3        Tokyo
4      Bangkok
Name: Ibukota, dtype: object

Matplotlib

  • Matplotlib adalah library Python untuk visualisasi data dengan dua dimensi
  • Bersifat open source dan tersedia di https://matplotlib.org/
  • Matplotlib berkaitan dengan NumPy dan Pandas
  • Jika library belum terpasang, tuliskan perintah instalasi: pip install matplotlib
  • Kemudian impor: import matplotlib.pyplot as plt
In [38]:
import matplotlib.pyplot as plt
In [39]:
year = [1980, 1990, 2000, 2010, 2020]
In [40]:
price = [2.5, 7.6, 9.7, 15.8, 22.9]
In [41]:
plt.plot(year, price)
plt.show()
In [42]:
plt.scatter(year,price)
Out[42]:
<matplotlib.collections.PathCollection at 0x1b681e999d0>
In [43]:
plt.bar(year,price)
Out[43]:
<BarContainer object of 5 artists>